<!-- 版权所属说明组件  -->
<template lang="html">
  <div style="width:100%">
    <!-- <a :href='"http://www.xdocin.com/xdoc?_func=to&_format=html&_cache=1&_xdoc="+ host + "/nf/standard/file/view/previewPdfFile?filePath=" + src' target="_blank" rel="nofollow">XDOC</a> -->
    <div style="width:100%" v-if="src != ''">
      <!-- <iframe class="pdf" :src="host + '/nf/standard/file/view/previewPdfFile?filePath=' + src" frameborder="0" :hidden="isIE"></iframe> -->
      <div v-if="!ispic" style="width:100%">
        <!-- <iframe class="pdf" v-if="ispdf" :src="'http://47.96.186.67:8012/onlinePreview?url=' + baseSrc + src" frameborder="0" :hidden="isIE"></iframe> -->
        <iframe class="pdf" v-if="ispdf" :src="baseSrc + src" frameborder="0" :hidden="isIE || isdd"></iframe>
        <iframe class="pdf bg-ww" v-else :src="host + '/nf/standard/file/view/previewWordOrExcel?filePath=' + src" frameborder="0" :hidden="isIE || isdd"></iframe>
        <div class="ietip" v-if="isIE">
          <p>当前浏览器不支持PDF文件预览，请下载后查看</p>
          <p>推荐使用Chrome浏览器进行文件预览</p>
        </div>
        <div class="ietip" v-if="isdd">
          <p>钉钉浏览器不支持PDF文件预览，请下载后查看</p>
          <p>推荐使用Chrome浏览器进行文件预览</p>
        </div>
        
        <div align="right">
          <a :href="baseSrc + src" v-if="!ispdf" target="_blank" download="download">下载</a>
          <el-button v-if="isdd" type="primary" @click="open(baseSrc + src)">默认浏览器打开</el-button>
          <el-button v-if="isdd" type="primary" @click="downfileDD(baseSrc + src)">下载文件</el-button>
        </div>
      </div>
      <div align="center" v-else>
        <img :src="baseSrc + src" :alt="baseSrc + src" />
      </div>
    </div>
    <div class="ietip" v-else>
      <p>无附件</p>
    </div>
    <!-- <iframe :src='"https://view.officeapps.live.com/op/view.aspx?src=" + host + "/nf/standard/file/view/previewPdfFile?filePath=" + src' width='100%' height='600px' frameborder='0'> -->
    </iframe>
  </div>
</template>

<script>
  // import config from "@/config"
  export default {
    props: {
      src: {
        type: String,
        default: ""
      }
    },
    data() {
      return {
        // baseSrc: config.staticUrl,
        baseSrc: '',
        isIE: false,
        isdd: false,
        pageCount: 0,
        host: "",
        page: 1,
        currentPage: 1,
        ispdf: false,
        ispic: false
      };
    },
    mounted() {
      let _this = this;
      // this.host = this.$axios.defaults.baseURL;
      this.checkIE()
      console.log(this.src.split('.')[this.src.split('.').length - 1], '当前格式')
      if (this.src.split('.')[this.src.split('.').length - 1] == 'jpg' || this.src.split('.')[this.src.split('.').length - 1] == 'jpeg' || this.src.split('.')[this.src.split('.').length - 1] == 'png') {
        this.ispic = true
      } else {
        this.ispic = false
        if (this.src.split('.')[this.src.split('.').length - 1] == 'pdf') {
          this.ispdf = true
        } else {
          this.ispdf = false
        }
      }
      this.$dd.ready(() => {
        _this.isdd = true
      })
    },
    watch: {
      src(o, n) {
        console.log(n.split('.')[n.split('.').length - 1], '预览文件格式')
        if (this.src.split('.')[this.src.split('.').length - 1] == 'jpg' || this.src.split('.')[this.src.split('.').length - 1] == 'jpeg' || this.src.split('.')[this.src.split('.').length - 1] == 'png') {
          this.ispic = true
        } else {
          this.ispic = false
          if (this.src.split('.')[this.src.split('.').length - 1] == 'pdf') {
            this.ispdf = true
          } else {
            this.ispdf = false
          }
        }
        this.page = 1;
        this.currentPage = 1;
        this.pageCount = 0;
      }
    },
    methods: {
      downfileDD(src){
        let _this = this;
        this.$dd.ready(() => {
          _this.$dd.biz.util.downloadFile({
              url: src,
              name: 'PDF文件.pdf', 
              onProgress: function(msg){
                // 文件下载进度回调
              },
              onSuccess : function(result) {
                  /*
                    true
                  */
              },
              onFail : function() {
                  alert('下载失败')
              }
          })
        })
      },
      open(src){
        let _this = this;
        this.$dd.ready(() => {
          _this.$dd.biz.util.openLink({
              url:src,
              onSuccess : function(result) {
                  /**/
              },
              onFail : function(err) {}
          })
        })
      },
      downloads() {
        console.log(this.src.split('fileUrl=')[1])
        window.open(
          this.$http.defaults.baseURL +
          "/v1/file/download?fileUrl=" +
          encodeURIComponent(this.src.split('fileUrl=')[1].replace(/\//g, "\\"))
        );
      },
      checkIE() {
        if (!!window.ActiveXObject || "ActiveXObject" in window) {
          console.log('IE')
          this.isIE = true;
          return true;
        } else {
          console.log('noIE')
          this.isIE = false;
          return false;
        }
      },
      nextPage() {
        if (this.page == this.pageCount) {
          return false;
        }
        this.page++;
      },
      pevPage() {
        if (this.page == 1) {
          return false;
        }
        this.page--;
      },
      pefload() {
        console.log("加载了");
      },
      myBrowser() {
        var userAgent = navigator.userAgent; //取得浏览器的userAgent字符串
        var isOpera = userAgent.indexOf("Opera") > -1;
        if (isOpera) {
          return "Opera";
        } //判断是否Opera浏览器
        if (userAgent.indexOf("Firefox") > -1) {
          return "FF";
        } //判断是否Firefox浏览器
        if (userAgent.indexOf("Chrome") > -1) {
          return "Chrome";
        }
        if (userAgent.indexOf("Safari") > -1) {
          return "Safari";
        } //判断是否Safari浏览器
        if (
          userAgent.indexOf("compatible") > -1 &&
          userAgent.indexOf("MSIE") > -1 &&
          !isOpera
        ) {
          if (userAgent.indexOf("MSIE 6.0") > -1) {
            return "IE6";
          }
          if (userAgent.indexOf("MSIE 7.0") > -1) {
            return "IE7";
          }
          if (userAgent.indexOf("MSIE 8.0") > -1) {
            return "IE8";
          }
          if (userAgent.indexOf("MSIE 9.0") > -1) {
            return "IE9";
          }
          if (userAgent.indexOf("MSIE 10.0") > -1) {
            return "IE10";
          }
          return "IE";
        } //判断是否IE6-9浏览器
        if (
          userAgent.toLowerCase().indexOf("trident") > -1 &&
          userAgent.indexOf("rv") > -1 &&
          !isOpera
        ) {
          if (userAgent.indexOf("rv:10.0") > -1) {
            return "IE10";
          }
          if (userAgent.indexOf("rv:11.0") > -1) {
            return "IE11";
          }
          return "IE11";
        } //判断是否IE10-11浏览器
        else {
          return userAgent;
        }
      }
    }
  };
</script>

<style lang="css" scoped>
  .pagebtn {
    cursor: pointer;
    color: cornflowerblue;
    margin: 0 10px;
    -moz-user-select: none;
    -webkit-user-select: none;
    -ms-user-select: none;
    -khtml-user-select: none;
    user-select: none;
  }
  .dispagebtn {
    cursor: default;
    color: #ccc;
  }
  .pdf {
    width: 100%;
    margin: 10px 0;
    min-height: 800px;
    background-color: #ccc;
    overflow: hidden;
  }
  .bg-ww {
    background-color: #fff;
  }
  .center {
    position: relative;
    text-align: center;
    top: -60px;
  }
  .ietip {
    text-align: center;
    line-height: 40px;
  }
</style>